<?php

	error_reporting(0);
	session_start();
	error_reporting(1);

	global $site_root;
	global $showUser;
	
	require_once ($site_root . "common/PinSQL.obj");
	$pinSQL = new PinSQL();
	
	$index = 1;
	$total = 0;
	
	if (!$showUser)
	{
		$id = $_SESSION['id'];
		$pinSQLm = new PinSQL();
		$pinSQLm->Query("SELECT * FROM memberdata WHERE id='$id'");
		$rowm = $pinSQLm->FetchNextRow();
		
		if (!$rowm['tagged_users'])
		{
			?>
			<div class="redtext">You do not currenly have any users tagged.
			<br />Tag a user from their profile or by clicking the corresponding link under "<a href="/members/frame.php?page=viewusers">View Users</a>."</div>
			<p />
			<?php
		}
		$taggedUsers = explode(',', $rowm['tagged_users']);
		
	}

	if (!$showUser)
	{
		$pinSQL->Query("SELECT * FROM activity ORDER BY date DESC");
	} else {
		$pinSQL->Query("SELECT * FROM activity WHERE by_user='$showUser' OR param_1='$showUser' ORDER BY date DESC LIMIT 0, 5");
	}
	
	$pinSQL2 = new PinSQL();
	
	if ( (!$pinSQL->GetNumRows()) AND ($showUser) )
	{
		echo "$showUser has no recent activity.";
	}
	
	while ( ( $row = $pinSQL->FetchNextRow() ) AND ( $total <= 15) )
	{		
		
		// variables associated with a typical event in "activity" table
		$id = $row['id'];
		$event_type = $row['event_type'];
		$by_user = $row['by_user'];
		$param_1 = $row['param_1'];
		$param_2 = $row['param_2'];
		$param_3 = $row['param_3'];
		$param_4 = $row['param_4'];
		$url_1 = $row['url_1'];
		$url_2 = $row['url_2'];
		$url_3 = $row['url_3'];
		$url_4 = $row['url_4'];
		$param_long = $row['param_long'];
		$date = $row['date'];
		
		$prettydate = date( 'M j', strtotime($date) );
     	$prettytime = date( 'g:ia', strtotime($date) );
     	
     	if (!$showUser)
     	{
			$isTagged = FALSE;
			for ($i=0; $i<sizeof($taggedUsers); $i++)
			{
				
				if ( ($taggedUsers[$i] == $by_user) OR (!$by_user) )
				{ $isTagged = TRUE; }
			}
		} else {
			$isTagged = TRUE;
		}
		
		if ( ($isTagged) OR ($event_type == "new-user") OR (($param_1 == $showUser) AND ($showUser)) OR ($by_user == $_SESSION['username']) )
		{
			$total++;

?>

<div class="event">
	<img src="/img/icons/update-<?php echo $event_type; ?>.png" />
<?php	

		$showImage = FALSE;
		
		$realname = $pinSQL->GetUserField($by_user, "firstname");
		if ($realname == "")
			$realname = $by_user;
		
		if ($event_type != "new-news")
		{
			echo "<a href=\"/members/frame.php?page=profile&user=$by_user\">$realname</a> ";
			echo "<a href=\"$url_1\">";
		} else {
			echo "$param_1";
		}
		
     	switch ($event_type)
     	{
	     	case "new-buzz":
					$realname2 = $pinSQL->GetFirstName($param_1);   	
		     	echo "wrote on $realname2's buzz.";
				break;
	     	case "new-user":
		     	echo "joined The Pin Project!";
				break;
	     	case "profile-edit":
		     	echo "made profile changes.";
				break;
	     	case "profile-newpic":
		     	echo "uploaded a new user picture.";
				break;
	     	case "new-song":
	     		$name = $pinSQL->GetMediaField("songdata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
		     	echo "uploaded a new song entitled \"$name\":";
				break;
	     	case "new-picture":
	     		$name = $pinSQL->GetMediaField("picturedata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
		     	echo "uploaded a new picture entitled \"$name\":";
		     	$showImage = TRUE;
				break;
	     	case "new-entry":
	     		$name = $pinSQL->GetMediaField("blogdata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
		     	echo "posted a new blog entry entitled \"$name\":";
				break;
	     	case "new-song-comment":
	     		$name = $pinSQL->GetMediaField("songdata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
	     		$artist = $pinSQL->GetMediaField("songdata", $param_1, "artist");
					$realname2 = $pinSQL->GetFirstName($artist); 
		     	echo "commented on $realname2's song \"$name\":";
				break;
	     	case "new-picture-comment":
	     		$name = $pinSQL->GetMediaField("picturedata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
	     		$artist = $pinSQL->GetMediaField("picturedata", $param_1, "artist");
					$realname2 = $pinSQL->GetFirstName($artist);    		
		     	echo "commented on $realname2's picture \"$name\":";
		     	$showImage = TRUE;
				break;
	     	case "new-entry-comment":
	     		$name = $pinSQL->GetMediaField("blogdata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
	     		$artist = $pinSQL->GetMediaField("blogdata", $param_1, "artist");
					$realname2 = $pinSQL->GetFirstName($artist);     		
		     	echo "commented on $realname2's blog entry \"$name\":";
				break;
	     	case "new-song-rating":
	     		$name = $pinSQL->GetMediaField("songdata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
	     		$artist = $pinSQL->GetMediaField("songdata", $param_1, "artist");
					$realname2 = $pinSQL->GetFirstName($artist);  		
	     		switch ($param_2)
	     		{
		     		case 1: echo "despises"; break;
		     		case 2: echo "really doesn't like"; break;
		     		case 3: echo "sort of likes"; break;
		     		case 4: echo "loves"; break;
		     		case 5: echo "was blown away by"; break;
	     		}
		     	echo " $realname2's song \"$name,\" rating it $param_2/5. <br /><img src=\"/common/gallery/img/gauge_$param_2.png\" border=0/>";
				break;
	     	case "new-picture-rating":
	     		$name = $pinSQL->GetMediaField("picturedata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
	     		$artist = $pinSQL->GetMediaField("picturedata", $param_1, "artist");
					$realname2 = $pinSQL->GetFirstName($artist);    		
	     		switch ($param_2)
	     		{
		     		case 1: echo "loathes"; break;
		     		case 2: echo "basically hates"; break;
		     		case 3: echo "kind of likes"; break;
		     		case 4: echo "really loves"; break;
		     		case 5: echo "would practically marry"; break;
	     		}
		     	echo " $realname2's picture \"$name,\" rating it $param_2/5. <br /><img src=\"/common/gallery/img/gauge_$param_2.png\" border=0/>";
		     	$showImage = TRUE;
				break;
	     	case "new-entry-rating":
	     		$name = $pinSQL->GetMediaField("blogdata", $param_1, "name");
	     		$name = ($name) ? $name : "(untitled)";
	     		$artist = $pinSQL->GetMediaField("blogdata", $param_1, "artist");
					$realname2 = $pinSQL->GetFirstName($artist);  		
	     		switch ($param_2)
	     		{
		     		case 1: echo "loathes"; break;
		     		case 2: echo "basically hates"; break;
		     		case 3: echo "kind of likes"; break;
		     		case 4: echo "really loves"; break;
		     		case 5: echo "would practically marry"; break;
	     		}
		     	echo " $realname2's blog entry \"$name,\" rating it $param_2/5. <br /><img src=\"/common/gallery/img/gauge_$param_2.png\" border=0/>";
				break;
     	}
     	echo "</a>";
     	
     	echo " <span class=\"subtle\">$prettydate at $prettytime</span>";
     	
		if ( ($_SESSION['level'] == "admin") AND (!$showUser)) {
			
			?>
	<a href="javascript:AJAXActionL('do_action.php?actionname=deletestory&id=<?php echo $id; ?>', <?php echo $index; ?>)"><img src="/img/icons/x.png" border=0/></a>
<?php include "common/PinAJAXLine.php"; }
     	
     	if ($showImage)
     	{
	     	
	     	$artist = $pinSQL2->GetMediaField("picturedata", $param_1, "artist");
	     	$filename = $pinSQL2->GetMediaField("picturedata", $param_1, "filename");
	     	$thumburl = $pinSQL2->GetMediaField("picturedata", $param_1, "thumburl");
	     	
			$oldDimensions = getimagesize($site_root . "members/userData/$artist/pictures/" . $thumburl);
			$oldWidth = $oldDimensions[0];
			$oldHeight = $oldDimensions[1];

			$heightFactor = $oldHeight / $oldWidth;
			$newWidth = 100;
			$newHeight = $newWidth * $heightFactor;

			?>

<blockquote>
<a href="/members/userData/<?php echo $artist; ?>/pictures/<?php echo $filename; ?>" class="highslide" onclick="return hs.expand(this)">
    <img src="/members/userData/<?php echo $artist; ?>/pictures/<?php echo $thumburl; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>" border=0>
</a>
</blockquote>

<?php
     	}
     	
     	if ($param_long)
     	{
	     	if ( (strlen($param_long) > 150) AND ($event_type != "new-news")) 
	     	{
		     	$param_long = substr($param_long, 0, 147) . "...";
	     	}
	     	echo nl2br("<blockquote>$param_long</blockquote>");
     	}
		
?>
</div>

<?php	
		}
	}
?>
